package com.hanhai.zrb.api.facade.impl;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hanhai.common.util.MD5Util;
import com.hanhai.zrb.api.biz.result.UserCenterResult;
import com.hanhai.zrb.api.biz.service.UserRegisterCheckService;
import com.hanhai.zrb.api.biz.service.UserRegisterService;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.api.biz.service.ValidateCodeSendService;
import com.hanhai.zrb.api.client.facade.UserModifyFacade;
import com.hanhai.zrb.api.client.result.UserModifyResult;
import com.hanhai.zrb.model.common.ValidateCodeSend.BizTypeEnum;
import com.hanhai.zrb.model.common.ValidateCodeSend.SendSMSTypeEnum;
import com.hanhai.zrb.model.user.User;

@WebService(endpointInterface = "com.hanhai.zrb.api.client.facade.UserModifyFacade", serviceName = "UserModifyFacade")

@Service("userModifyFacade")
public class UserModifyFacadeImpl implements UserModifyFacade{
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private UserService userService;
	@Resource
	private UserRegisterService userRegisterService;
	@Resource
	private UserRegisterCheckService userRegisterCheckService;
	@Resource
	private ValidateCodeSendService validateCodeSendService;
	
	private static String regpw = "^[^\\s]{6,16}$";
	private static String regMobile = "^[1][1,2,3,4,5,6,7,8,9][0-9]{9}$";
	
	/*public UserModifyFacade() {
		userService  = (UserService)SpringContextUtil.getBean("userService");
		userRegisterService = (UserRegisterService)SpringContextUtil.getBean("userService");
		userRegisterCheckService = (UserRegisterCheckService)SpringContextUtil.getBean("userService");
		validateCodeSendService = (ValidateCodeSendService)SpringContextUtil.getBean("validateCodeSendService");
		
	}
	*/
	/**
	 * 发送密码找回短信验证码
	 * 
	 * @param mobile
	 * @return
	 */
	public UserModifyResult sendFindPwdSmsCheckCode(String mobile){
		UserModifyResult  result= new UserModifyResult();
		if(StringUtils.isBlank(mobile)||mobile.matches(regMobile)){
			result.setMsg("手机号不正确");
			return result;
		}
		
		UserCenterResult mobileNoExitResult = userRegisterCheckService.checkMobileOrEmailExist(mobile, null);
		if(mobileNoExitResult.isSuccess()){
			result.setMsg("手机号码未注册，请注册");
		}
		
		BizTypeEnum type  = BizTypeEnum.SmsFindPassword;
		int codeType = SendSMSTypeEnum.FIND_PASSWORD.getIndex();
		UserCenterResult sendResult = userRegisterService.sendSMSCode(mobile, type.name(), type, codeType);
		if( !sendResult.isSuccess()){
			result.setMsg(sendResult.getMsg());
			return result;
		}
		result.setSuccess(true);
		result.setMsg("短信发送成功");
		return result;
		
	}
	
	/**
	 * 找回密码，通过手机号和验证码，更换新密码
	 * 
	 * @param mobile
	 * @param smsCode
	 * @param newPwd
	 * @return
	 */
	public UserModifyResult changePwdByMobile(String mobile,String smsCode,String newPwd){
		UserModifyResult  result= new UserModifyResult();
		if(StringUtils.isBlank(mobile)||mobile.matches(regMobile)){
			result.setMsg("手机号不正确");
			return result;
		}
		if(!(newPwd.trim().matches(regpw))){
			result.setMsg("密码不符合规则");
			return result;
		}
		UserCenterResult checkResult = userRegisterService.checkSMSCode(mobile, smsCode, BizTypeEnum.SmsFindPassword);
		if(!checkResult.isSuccess()){
			result.setMsg(checkResult.getMsg());
			return result;
		}
		
		userService.updatePwdByMobile( mobile, newPwd);
		result.setMsg("密码修改成功");
		
		result.setSuccess(true);
		logger.info("通过手机修改密码成功:mobile="+mobile);
		return result;
	}

	/**
	 * 修改用户的登陆密码，
	 * 需要校验原密码，通常是用户登陆后自助修改密码的额时候调用
	 * @param uid
	 * @param oldPwd
	 * @param newPwd
	 * @return
	 */
	public UserModifyResult changePwd(Long uid,String oldPwd,String newPwd){
		UserModifyResult result = new UserModifyResult();
		
		User user = userService.findById(uid);
		if(user==null){
			result.setMsg("用户不存在");
			return result;
		}
		String sh1oldPwd = MD5Util.sha1(oldPwd);
		if(!StringUtils.equals(user.getPassword(), sh1oldPwd)){
			result.setMsg("原密码错误");
			return result;
		}
		user.setPassword(newPwd);
		userService.updatePassword(user);
		result.setSuccess(true);
		logger.info("用户修改密码成功:用户id="+user.getUid());
		return result;
		
	}
	
	
	
	
	/**
	 * 用户给需要更换的新手机校验码短息
	 * 
	 * @param uid
	 * @return
	 */
	public UserModifyResult sendChangeMobileSmsCode(Long uid,String newMobile){
		UserModifyResult result = new UserModifyResult();
		User user = userService.findById(uid);
		if(user==null){
			result.setMsg("用户不存在");
			return result;
		}
		//String mobile = user.getUmobile();
		BizTypeEnum type  = BizTypeEnum.SmsModifyMobile;
		int codeType = SendSMSTypeEnum.MODIFY_PHONE.getIndex();
		UserCenterResult sendResult = userRegisterService.sendSMSCode(newMobile, type.name(), type, codeType);
		
		if(!sendResult.isSuccess()){
			result.setMsg(sendResult.getMsg());
			return result;
		}
		
		result.setSuccess(true);
		logger.info("成功发送修改手机验证码短信:用户id="+user.getUid());
		return result;
	}
	
	/**
	 * 用户修改手机验证并执行
	 * 
	 * 根据新新手机的短信码更换手机
	 * 
	 * @param uid
	 * @param newMobile
	 * @param smsCode
	 * @return
	 */
	public UserModifyResult changeMobile(Long uid,String newMobile,String smsCode){
		UserModifyResult result = new UserModifyResult();
		User user = userService.findById(uid);
		if(user==null){
			result.setMsg("用户不存在");
			return result;
		}
		
		UserCenterResult checkResult = userRegisterService.checkSMSCode(newMobile, smsCode, BizTypeEnum.SmsModifyMobile);
		if(!checkResult.isSuccess()){
			result.setMsg(checkResult.getMsg());
			return result;
		}
		
		UserCenterResult mobileNoExitResult = userRegisterCheckService.checkMobileOrEmailExist(newMobile, null);
		if(!mobileNoExitResult.isSuccess()){
			result.setMsg("手机号已经注册");
			return result;
		}
		
		
		user.setUmobile(newMobile);
		userService.updateMobile(user);
		
		result.setSuccess(true);
		logger.info("用户修改密码成功:用户id="+user.getUid());
		return result;
	}
	
}
